package com.cqc.jdk8.Jlang;

import com.cqc.eb.UserEB;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectTest {

    @Test
    public void f1() throws Exception {
        UserEB eb = new UserEB();
        eb.setAge(20);
        eb.setSalary(99.99);
        eb.setAddress("山东");
        //获得所有的字段
        Field[] declaredFields = UserEB.class.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(eb);
            System.out.println("fieldName = " + fieldName+",value = " + value);
        }

        //获得声明的方法
        Method[] methods = UserEB.class.getDeclaredMethods();
        for (Method method : methods) {
            String methodName = method.getName();
            if("setAddress".equals(methodName)){
                method.setAccessible(true);
                Object invoke = method.invoke(eb,"北京");
                System.out.println("getAddress = " + invoke + ",Address="+eb.getAddress());
            }
        }
    }
}
